package com.tang.framework.config.jackson;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.deser.std.NumberDeserializers;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Objects;

/**
 * @Author tang jun liang
 * @Date 2023/10/25 10:17
 */
@JacksonStdImpl
public class CustomizeBigDecimalDeserializer extends NumberDeserializers.BigDecimalDeserializer {

    public static final CustomizeBigDecimalDeserializer instance = new CustomizeBigDecimalDeserializer();

    @Override
    public BigDecimal deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        BigDecimal bigDecimal = super.deserialize(p, ctxt);
        if (Objects.nonNull(bigDecimal)){
            return bigDecimal;
        }
        return BigDecimal.ZERO;
    }

    @Override
    public BigDecimal deserialize(JsonParser p, DeserializationContext ctxt, BigDecimal intoValue) throws IOException {
        BigDecimal bigDecimal = super.deserialize(p, ctxt, intoValue);
        if (Objects.nonNull(bigDecimal)){
            return bigDecimal;
        }
        return BigDecimal.ZERO;
    }
}
